import request from '../../utils/request'

let startY = 0,
  moveY = 0,
  moveDistance = 0

Page({

  /**
   * 页面的初始数据
   */
  data: {
    coverTransform: "translateY(0rpx)",
    coveTransition: '',
    // 用户个人信息
    userInfo: {},
    // 用户最近播放记录
    recentPlayList: []
  },

  handleTouchStart(e) {
    startY = e.touches[0].clientY
  },
  handleTouchMove(e) {
    moveY = e.touches[0].clientY
    moveDistance = moveY - startY
    // console.log(moveDistance);
    if (moveDistance < 0) {
      return;
    }
    if (moveDistance > 80) {
      moveDistance = 80
    }
    this.setData({
      coverTransform: `translateY(${moveDistance}rpx)`,
      coveTransition: ''
    })
  },
  handleTouchEnd(e) {
    this.setData({
      coverTransform: "translateY(0rpx)",
      coveTransition: 'transform 1s linear'
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const userInfo = wx.getStorageSync('userInfo')
    if(userInfo) {
      this.setData({
      userInfo: JSON.parse(userInfo)
      })
    }
    this.getRecentPlayList(this.data.userInfo.userId)
  },

  async getRecentPlayList(userId) {
    const result = await request('/user/record', {
      uid: userId,
      type: 0
    })
    // console.log(result)
    const recentPlayList = result.allData.slice(0, 10)
    // console.log(recentPlayList)
    this.setData({
      recentPlayList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})